﻿using System;
using System.Collections.Generic;
using Basic.Model.Entity;
using NHibernate;
using NHibernate.Criterion;

namespace Basic.Model.EntityManager
{
    public class AdminManager
    {
        public static void Add(ISession session, Admin an)
        {
            session.Save(an);
        }

        public static void Delete(ISession session, Admin an)
        {
            session.Delete(an);
        }

        public static void Update(ISession session, Admin an)
        {
            session.Update(an);
        }

        public static Admin Get(ISession session, int itemId)
        {
            return session.Get<Admin>(itemId);
        }

        public static Admin GetByCondition(ISession session, int adminId, string adminName, string adminPass)
        {
            ICriteria c = session.CreateCriteria(typeof(Admin));
            if (adminId >= 0)
                c.Add(Expression.Eq("adminId", adminId));
            if (!String.IsNullOrEmpty(adminName))
                c.Add(Expression.Eq("adminName", adminName));
            if (!String.IsNullOrEmpty(adminPass))
                c.Add(Expression.Eq("adminPass", adminPass));
            ICriteria c1 = c.CreateCriteria("adminGroup", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            c1.SetFetchMode("roles", FetchMode.Select);
            return c.UniqueResult<Admin>();
        }

        public static IList<Admin> GetAll(ISession session)
        {
            ICriteria c = session.CreateCriteria(typeof(Admin));
            return c.List<Admin>();
        }

        public static IList<Admin> GetAllByCondition(ISession session, string adminName, int[] adminIds, int groupId, int rowIndex, int rowCount)
        {
            ICriteria c = session.CreateCriteria(typeof(Admin));
            if (!String.IsNullOrEmpty(adminName))
                c.Add(Expression.Eq("adminName", adminName));
            if (adminIds != null)
                c.Add(Expression.In("adminId", adminIds));
            ICriteria c1 = c.CreateCriteria("adminGroup", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            if (groupId >= 0)
                c1.Add(Expression.Eq("groupId", groupId));
            if (rowIndex >= 0)
                c.SetFirstResult(rowIndex);
            if (rowCount >= 0)
                c.SetMaxResults(rowCount);
            return c.List<Admin>();
        }
        public static int GetAllByConditionCount(ISession session, string adminName, int[] adminIds, int groupId)
        {
            ICriteria c = session.CreateCriteria(typeof(Admin));
            if (!String.IsNullOrEmpty(adminName))
                c.Add(Expression.Eq("adminName", adminName));
            if (adminIds != null)
                c.Add(Expression.In("adminId", adminIds));
            if (groupId >= 0)
                c.Add(Expression.Eq("adminGroup.groupId", groupId));
            return (int)c.SetProjection(Projections.Count("adminId")).UniqueResult();
        }
    }
}
